<?php
namespace app\admin\controller;

use app\common\base\AuthenticatedController;
use app\common\base\Form;
use think\Db;
use think\Error;

/**
 * 后台文章管理控制器
 * Class ArticleController
 * @package app\admin\controller
 */
class ArticleController extends AuthenticatedController {


    /**
     * 添加文章接口
     * @return \think\response\Json
     */
    public function Add() {

        Form::Validate([
            'title'                  =>  'require',
            'content'                =>  'require',
        ]);

        $data = [
            'title'                 =>  input('title'),
            'content'               =>  input('content'),
            'cover'                 =>  input('cover'),
            'classify'              =>  input('classify', ''),
        ];

        $result = Db::table('article')->insert($data);
        if (!$result) {
            return Error('添加失败');
        }

        return Success('添加设备成功');

    }


    /**
     * 删除文章接口
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function Delete() {

        Form::Validate([
            'id'  =>  'require',
        ]);

        if (!Db::table('article')->where('id', input('id'))->delete()) {
            return Error('删除失败');
        }

        return Success('删除成功');

    }


    /**
     * 修改文章接口
     * @return \think\response\Json
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function Modify() {

        Form::Validate([
            'id'                    =>  'require|integer',
            'title'                  =>  'require',
            'content'                =>  'require',
        ]);

        $data = [
            'title'                 =>  input('title'),
            'content'               =>  input('content'),
            'cover'                 =>  input('cover'),
            'classify'              =>  input('classify', ''),
        ];

        if (!Db::table('article')->where('id', input('id'))->update($data)) {
            return Error('更新失败');
        }

        return Success('更新成功');
    }


    /**
     * 获取文章列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetList() {
        $result = Db::table('article')->select();

        return Success('获取成功', $result);
    }


    /**
     * 根据ID获取文章
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function GetOne() {
        Form::Validate([
            'id'    =>  'require|integer'
        ]);

        $result = Db::table('article')->where('id', input('id'))->find();
        if (!$result) {
            return Error('获取失败');
        }

        return Success('获取成功', $result);

    }

}